import ToolRequest from '@/base/newToolAxios';
import RequestOption from '@/base/RequestOption';
import { getActToolUrl } from '@/base/constant';
const tr = new ToolRequest();

export default class TeachingClassDao {
  /**
   * 根据课程ID列表获取班级、小组和学生
   * @param courseId 课程ID
   * @return 班级列表
   */
  async findClassByCourseId(courseId: number) {
    console.log(`[ClassDAO] 获取课程(${courseId})的班级信息`);
    const ro = new RequestOption();
    
    try {
      const response = await tr.get(
        getActToolUrl(),
        {
          action: 'findProjectClassesAndTeamsByCourseId',
          courseId: courseId
        },
        ro
      );
      
      console.log(`[ClassDAO] 获取班级响应: code=${response?.data?.code}, 班级数量=${response?.data?.data?.length || 0}`);
      
      if (response?.data?.code !== 200) {
        console.warn(`[ClassDAO] 获取班级接口返回错误: ${response?.data?.msg || '未知错误'}`);
      }
      
      return response;
    } catch (error) {
      console.error('[ClassDAO] 获取班级信息异常:', error);
      throw error;
    }
  }

  /**
   * 添加班级
   * @param courseId 课程ID
   * @param className 班级名称
   * @returns 添加结果
   */
  async addClass(courseId: number, className: string) {
    console.log(`[ClassDAO] 添加班级: ${className} 到课程(${courseId})`);
    const ro = new RequestOption();
    
    try {
      const response = await tr.post(
        getActToolUrl(),
        {
          action: 'addProjectClass',
          courseId: courseId,
          className: className
        },
        ro
      );
      
      console.log(`[ClassDAO] 添加班级响应: code=${response?.data?.code}, msg=${response?.data?.msg || '操作成功'}`);
      
      if (response?.data?.code !== 200) {
        console.warn(`[ClassDAO] 添加班级接口返回错误: ${response?.data?.msg || '未知错误'}`);
      }
      
      return response;
    } catch (error) {
      console.error('[ClassDAO] 添加班级异常:', error);
      throw error;
    }
  }
  
  /**
   * 删除班级
   * @param classId 班级ID
   * @returns 删除结果
   */
  async deleteClass(classId: number) {
    console.log(`[ClassDAO] 删除班级(${classId})`);
    const ro = new RequestOption();
    
    try {
      const response = await tr.post(
        getActToolUrl(),
        {
          action: 'deleteProjectClass',
          classId: classId
        },
        ro
      );
      
      console.log(`[ClassDAO] 删除班级响应: code=${response?.data?.code}, msg=${response?.data?.msg || '操作成功'}`);
      
      if (response?.data?.code !== 200) {
        console.warn(`[ClassDAO] 删除班级接口返回错误: ${response?.data?.msg || '未知错误'}`);
      }
      
      return response;
    } catch (error) {
      console.error('[ClassDAO] 删除班级异常:', error);
      throw error;
    }
  }

  /**
   * 添加学生到班级
   * @param classId 班级ID
   * @param studentIds 学生ID数组
   * @returns 添加结果
   */
  async addClassStudentRef(classId: number, studentIds: Array<number>) {
    // 确保studentIds是数组
    if (!Array.isArray(studentIds)) {
      console.error('[ClassDAO] studentIds必须是数组:', studentIds);
      throw new Error('studentIds必须是数组');
    }
    
    // 确保所有ID都是数字
    const validIds = studentIds.filter(id => typeof id === 'number');
    if (validIds.length !== studentIds.length) {
      console.warn('[ClassDAO] 学生ID列表中存在非数字值，已过滤');
    }
    
    console.log(`[ClassDAO] 添加${validIds.length}名学生到班级(${classId}), 学生IDs: ${validIds.join(',')}`);
    const ro = new RequestOption();
    
    try {
      const response = await tr.post(
        getActToolUrl(),
        {
          action: 'addClassStudentRef',
          classId: classId,
          studentIds: JSON.stringify(validIds) // 以JSON格式发送数组
        },
        ro
      );
      
      console.log(`[ClassDAO] 添加学生到班级响应: code=${response?.data?.code}, msg=${response?.data?.msg || '操作成功'}`);
      
      if (response?.data?.code !== 200) {
        console.warn(`[ClassDAO] 添加学生到班级接口返回错误: ${response?.data?.msg || '未知错误'}`);
      }
      
      return response;
    } catch (error) {
      console.error('[ClassDAO] 添加学生到班级异常:', error);
      throw error;
    }
  }

  /**
   * 从班级中删除学生
   * @param classId 班级ID
   * @param studentIds 学生ID数组
   * @returns 删除结果
   */
  async deleteClassStudentRef(classId: number, studentIds: Array<number>) {
    console.log(`[ClassDAO] 从班级(${classId})删除${studentIds.length}名学生, 学生IDs: ${studentIds.join(',')}`);
    const ro = new RequestOption();
    
    try {
      const response = await tr.post(
        getActToolUrl(),
        {
          action: 'deleteClassStudentRef',
          classId: classId,
          studentIds: studentIds.join(',')
        },
        ro
      );
      
      console.log(`[ClassDAO] 删除学生响应: code=${response?.data?.code}, msg=${response?.data?.msg || '操作成功'}`);
      
      if (response?.data?.code !== 200) {
        console.warn(`[ClassDAO] 删除学生接口返回错误: ${response?.data?.msg || '未知错误'}`);
      }
      
      return response;
    } catch (error) {
      console.error('[ClassDAO] 删除学生异常:', error);
      throw error;
    }
  }
}
